//index.js
//获取应用实例
const app = getApp()
const common = require('../../utils/login.js');
Page({
  data: {
    video:[],
    article: [],
    variable: {
      userBtn: true
    },
    token: '',
    pic: [],
    forward: '../../image/forward.png',
    searchVal: '',
    indexImg: '../../image/index.png',
    closeImg: 'https://gitee.com/Anjina/vote-pic/raw/7b66d388a031fdd545ce9a4ffc6a31e74de6710c/assets/Combined%20Shape@2x.png',
    isInput: false
  },
  searchFor: function (e) {
    var that = this
    var value = e.detail.value
    if (value != null || value != 'undefined') {
      that.setData({
        isInput: true
      })
    }
    if (value === '') {
      that.setData({
        isInput: false
      })
    }
    that.setData({
      searchVal: value
    })
  },
  clear: function () {
    this.setData({
      searchVal: '',
      isInput: false
    })
    console.log(this.data.searchVal)
  },
  //事件处理函数
  tabView: function(e) {
    var type = e.currentTarget.dataset.type;
    switch(type) {
      case 'article':
        wx.switchTab({
          url: '../article-total/article-total',
        });
        break
      case 'video':
        wx.switchTab({
          url: '../video-total/video-total',
        })
        break
      case 'pic':
        wx.switchTab({
          url: '../pic-total/pic-total',
        })
        break
      default:
        console.log('tabViewError')
    }
  },
  toDetails: function(e) {
    let id = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    switch(type) {
      case 'pic':
        wx.navigateTo({
          url: '../pic-details/pic-details?id=' + id,
        })
        break
      case 'video':
        wx.navigateTo({
          url: '../video-details/video-details?id=' + id,
        })
        break
      case 'article':
        wx.navigateTo({
          url: '../article-details/article-details?id=' + id,
        })
      default:
        console.log('toDetailsError')
    }
  },
  toSearch: function () {
    wx.navigateTo({
      url: '../search/search?searchVal=' + this.data.searchVal,
    })
  },
  // 判断是否有用户信息
  isUserInfo() {
    let userInfo = wx.getStorageSync('userInfo');
    let userBtn = 'variable.userBtn';
    if (userInfo) {
      this.setData({
        [userBtn]: false
      })
    }
  },
  // 登录
  bindGetUserInfo(e) {
    let id = e.currentTarget.dataset.id;
    console.log(e);
    let that = this;
    let userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      common.wxLogin().then(data => {
        console.log(data);
        let userBtn = 'variable.userBtn';
        that.setData({
          token: data.token,
          [userBtn]: false
        })
        wx.setStorageSync('userInfo', data.token); // 保存用户信息
        that.Voting(id);  // 登录之后调用投票
      });
    }
  },
  // 点击投票
  toupiao (e) {
    let id = e.currentTarget.dataset.id
    console.log(id)
    this.Voting(id);
  },
  // 投票请求封装
  Voting: function (id) {
    let that = this;
    wx.request({
      url: 'https://m.feawin.com/vote/add/'+id,
      data: '',
      method: 'POST',      
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        // 'custom': JSON.stringify(header),
        'Authorization': 'Bearer ' + wx.getStorageSync('userInfo')
      },
      success: function (res) {
        console.log(res)
        wx.showToast({
          title: '投票成功!',
          icon: 'success',
          duration: 2000
        })
        that.req();
      },
      fail: function (res) { console.log(res)},
      complete: function (res) { },
    })
  },
  // 已投票
  Voted () {
    console.log('已投票');
  },
  req: function () {
    var that = this
    wx.request({
      url: 'https://m.feawin.com/vote/list/generate?activityId=1',
      method: 'get',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        // 'custom': JSON.stringify(header),
        'Authorization': 'Bearer ' + wx.getStorageSync('userInfo')
      },
      success(res) {
        var data = res.data.list
        console.log(data)
        var length = data.length
        var video=[],article=[],pic=[]
        for (let i=0;i<data.length;i++) {
          var contentType = data[i].contentType
          if(contentType === 'video') {
            video.push(data[i])
          }else if(contentType === 'text') {
            article.push(data[i])
          }
          else {
            pic.push(data[i])
          }
        }
        that.setData({
          video: video,
          article: article,
          pic: pic
        })
      }
    })
  },
  onLoad: function (options) {
    this.req();
    this.isUserInfo();
  }
})
